package models

import (
	"sport_booking/dao"
	"sport_booking/utils/message"
	"strconv"
)

type Geo struct {
	UUID       string  `json:"uuid,omitempty" db:"user_uuid"`
	ID         int64   `json:"id,omitempty" db:"geo_id"`
	Level      int64   `json:"level,omitempty" db:"geo_level"`
	ParentCode int64   `json:"parent_code,omitempty" db:"geo_parent_code"`
	AreaCode   int64   `json:"area_code,omitempty" db:"geo_area_code"`
	ZipCode    int64   `json:"zip_code,omitempty" db:"geo_zip_code"`
	CityCode   string  `json:"city_code,omitempty" db:"geo_city_code"`
	Name       string  `json:"name,omitempty" db:"geo_name"`
	ShortName  string  `json:"short_name,omitempty" db:"geo_short_name"`
	MergerName string  `json:"merger_name,omitempty" db:"geo_merger_name"`
	Pinyin     string  `json:"pinyin,omitempty" db:"geo_pinyin"`
	Lng        float64 `json:"lng,omitempty" db:"geo_lng"`
	Lat        float64 `json:"lat,omitempty" db:"geo_lat"`
}

/*
获取第一级省市
*/
func GetOneProvince() ([]Geo, int) {
	var geos []Geo
	sqlStr := "SELECT geo_area_code,geo_name,geo_short_name FROM bk_geos WHERE geo_level=0 ORDER BY geo_pinyin ASC"
	err := dao.DB.Select(&geos, sqlStr)
	if err != nil {
		return geos, message.ERROR
	}
	return geos, message.SUCCESS
}

/*
分级获取城市列表
*/
func GetOtherCity(geo *Geo) ([]Geo, int) {
	var geos []Geo
	sqlStr := "SELECT geo_area_code,geo_name,geo_short_name " +
		"FROM bk_geos " +
		"WHERE geo_level=? " +
		"AND CAST(geo_area_code AS CHAR) LIKE ? " +
		"ORDER BY geo_pinyin ASC"
	err := dao.DB.Select(&geos, sqlStr, geo.Level, strconv.FormatInt(geo.AreaCode, 10)+"%")
	if err != nil {
		return geos, message.ERROR
	}
	return geos, message.SUCCESS
}
